package shortcircuit.graphics;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.Display;
import shortcircuit.GameEngine;
import shortcircuit.engine.EndlessEngine;
import shortcircuit.engine.MainMenu;
import shortcircuit.engine.PuzzleEngine;

/**
 * This PopUpScreen is displayed at the end of a new mode.  In Puzzle Mode, the success
 * of the user is displayed, allowing for several options after the level is completed.
 * In Endless Mode, a Game Over message is displayed, allowing the user to Try Again or return
 * to the Main Menu.
 * @author Kevin Lewi
 *
 */
public class EndGameScreen extends PopUpScreen
{
	private boolean gameWon;
	private JButton nextLevelButton;
	private JButton tryAgainButton;
	private JButton quitButton;
	private JPanel buttonPanel;

	public EndGameScreen(int typeOfGame, boolean gameWon, GameEngine theEngine)
	{
		super(typeOfGame, theEngine);
		_Transparency = 1.0f;
		setLayout(null);
		this.gameWon = gameWon;
		buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setLayout(null);
		nextLevelButton = new JButton();
		nextLevelButton.addActionListener(new ButtonListener());
		nextLevelButton.setLocation(0, 20);
		nextLevelButton.setSize(70, 40);
		nextLevelButton.setBorder(null);
		nextLevelButton.setContentAreaFilled(false);
		tryAgainButton = new JButton();
		tryAgainButton.addActionListener(new ButtonListener());
		tryAgainButton.setLocation(0, 20);
		tryAgainButton.setSize(70, 40);
		tryAgainButton.setBorder(null);
		tryAgainButton.setContentAreaFilled(false);
		quitButton = new JButton();
		quitButton.addActionListener(new ButtonListener());
		quitButton.setLocation(85, 20);
		quitButton.setSize(70, 40);
		quitButton.setBorder(null);
		quitButton.setContentAreaFilled(false);
		if(typeOfGame == PopUpScreen.PUZZLE)
		{
			if(gameWon)
				buttonPanel.add(nextLevelButton);
			else
				buttonPanel.add(tryAgainButton);
		}
		if(typeOfGame == PopUpScreen.ENDLESS)
			buttonPanel.add(tryAgainButton);
		buttonPanel.add(quitButton);
		buttonPanel.setSize(160, 80);
		buttonPanel.setLocation(0, 40);
		add(buttonPanel);
	}


	public void paint(Graphics g)
	{
		switch(typeOfGame)
		{
		case 0:
			//NOTE: To adjust the locations of the images, all you have to do is adjust the
			//locations of their respective buttons defined in the default constructor
			if(gameWon)
			{
				g.drawImage(Display.levelCompleteImage, 0, 0, this);
				g.drawImage(Display.nextLevelImage, nextLevelButton.getLocation().x + buttonPanel.getLocation().x,  nextLevelButton.getLocation().y + buttonPanel.getLocation().y, this);
			}
			else
			{

				g.drawImage(Display.failureImage, 0, 0, this);
				g.drawImage(Display.tryAgainImage, tryAgainButton.getLocation().x + buttonPanel.getLocation().x, tryAgainButton.getLocation().y + buttonPanel.getLocation().y, this);
			}
			break;
		case 1:
			g.drawImage(Display.gameOverImage, 0, 0, this);
			g.drawImage(Display.tryAgainImage, tryAgainButton.getLocation().x + buttonPanel.getLocation().x, tryAgainButton.getLocation().y + buttonPanel.getLocation().y, this);
			break;
		}
		g.drawImage(Display.quitEndImage, quitButton.getLocation().x + buttonPanel.getLocation().x, quitButton.getLocation().y + buttonPanel.getLocation().y, this);
		doneLoading = true;
		theAnimator = null;
	}

	public class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == nextLevelButton)
			{
				_Engine._Display.clearScreens();
				new PuzzleEngine(_Engine._Display, ((PuzzleEngine) _Engine).getLevel() + 1);
			}
			if(e.getSource() == tryAgainButton)
			{
				_Engine._Display.clearScreens();
				if(typeOfGame == ENDLESS)
					new EndlessEngine(_Engine._Display);
				else
					new PuzzleEngine(_Engine._Display, ((PuzzleEngine) _Engine).getLevel());
			}
			if(e.getSource() == quitButton)
			{
				_Engine._Display.clearScreens();
				new MainMenu(_Engine._Display);
			}
		}
	}
}
